package com.haizhi.crm.sys.shiro;

import com.haizhi.crm.common.context.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Set;

/**
 * Created by chengmo on 2018/7/3.
 */
@Component
public class ShiroRedisCleaner {

    private static final String DEFAULT_SESSION_KEY_PREFIX = "shiro:session:";

    @Autowired
    private RedisTemplate redisTemplate;

    @PostConstruct
    public void cleanupOnStartup() {
        String pattern = DEFAULT_SESSION_KEY_PREFIX + Resource.getIPAndPort() + "*";
        Set<String> keys = redisTemplate.keys(pattern);
        for (String key : keys) {
            redisTemplate.delete(key);
        }
    }
}
